package cn.edu.scau.cmi.zeyang.controller;

import java.util.HashSet;

import org.omg.IOP.TAG_CODE_SETS;

import cn.edu.scau.cmi.zeyang.adapter.AdapterOfClass;
import cn.edu.scau.cmi.zeyang.adapter.AdapterOfObject;
import cn.edu.scau.cmi.zeyang.client.Main;
import cn.edu.scau.cmi.zeyang.composite.safe.SafeOrganization;
import cn.edu.scau.cmi.zeyang.composite.safe.SafePerson;
import cn.edu.scau.cmi.zeyang.domain.adapter.Target;
import cn.edu.scau.cmi.zeyang.interProgramming.Animal;
import cn.edu.scau.cmi.zeyang.interProgramming.Cat;
import cn.edu.scau.cmi.zeyang.interProgramming.Dog;
import cn.edu.scau.cmi.zeyang.multiton.Multiton;
import cn.edu.scau.cmi.zeyang.singletons.Singletons;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class MainController {
	@FXML
	private Button expSingle;
	@FXML
	private Button expMulti;
	@FXML
	private Button expSimple;
	@FXML
	private Button expFactory;
	@FXML
	private Button expBase;
	@FXML
	private Button expAbstract;
	@FXML
	private Button expAdClass;
	@FXML
	private Button expAdObject;
	@FXML
	private Button expComSafe;
	@FXML
	private Button expComCon;
	@FXML
	private Button expHibSpring;

	// 单例
	private int accout;
	private Text text = null;
	private TextField textField = null;

	// 多例
	private String ans = "";
	private HashSet<Multiton> multions = Multiton.getInstance();

	public void initialize() {
		expBase.setOnAction(e -> {
			Animal cat = new Cat();
			Animal dog = new Dog();
			cat.eat();
			dog.eat();
		});

		expSingle.setOnAction(event -> {
			Stage stage = new Stage();
			try {
				BorderPane root = new BorderPane();

				Button addButton = new Button("生成对象");
				accout = 1;
				text = new Text("暂未输入名字");
				textField = new TextField("暂未输入名字");
				addButton.setOnAction(e -> {
					Singletons.getInstances(textField.getText());
					text.setText("输出次数:" + accout++ + "," + Singletons.getInstances(textField.getText()).getName());
				});

				root.setCenter(text);
				root.setBottom(textField);
				root.setTop(addButton);

				Scene scene = new Scene(root, 400, 400);
				stage.setScene(scene);
				stage.setTitle("实验二：单例模式");
				stage.initOwner(((Node) event.getSource()).getScene().getWindow());
				stage.initModality(Modality.WINDOW_MODAL);
				stage.show();
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

		expMulti.setOnAction(event -> {
			Stage stage = new Stage();
			try {
				BorderPane root = new BorderPane();

				Button addButton = new Button("生成对象并输出");
				accout = 1;
				text = new Text("暂未输入名字");
				textField = new TextField("暂未输入名字");
				addButton.setOnAction(e -> {
					Multiton.addObject(textField.getText());
					ans = "输出次数" + accout++ + "\n";
					int account = 1;
					for (Multiton multiton : multions) {
						ans = ans + account++ + ":" + multiton.getName() + "\n";
					}
					text.setText(ans);
				});

				root.setCenter(text);
				root.setBottom(textField);
				root.setTop(addButton);

				Scene scene = new Scene(root, 400, 400);
				stage.setScene(scene);
				stage.setTitle("实验二：多例模式");
				stage.initOwner(((Node) event.getSource()).getScene().getWindow());
				stage.initModality(Modality.WINDOW_MODAL);
				stage.show();
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

		expSimple.setOnAction(event -> {
			Stage stage = new Stage();
			try {
				Pane root = FXMLLoader.load(getClass().getResource("../client/SimpleFactory.fxml"));
				Scene scene = new Scene(root);
				stage.setScene(scene);
				stage.setTitle("实验三：简单工厂模式");
				stage.initOwner(((Node) event.getSource()).getScene().getWindow());
				stage.initModality(Modality.WINDOW_MODAL);
				stage.show();
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

		expFactory.setOnAction(event -> {
			Stage stage = new Stage();
			try {
				Pane root = FXMLLoader.load(getClass().getResource("../client/FactoryModule.fxml"));
				Scene scene = new Scene(root);
				stage.setScene(scene);
				stage.setTitle("实验三：工厂模式");
				stage.initOwner(((Node) event.getSource()).getScene().getWindow());
				stage.initModality(Modality.WINDOW_MODAL);
				stage.show();
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

		expAbstract.setOnAction(event -> {
			Stage stage = new Stage();
			try {
				Pane root = FXMLLoader.load(getClass().getResource("../client/AbstractFactory.fxml"));
				Scene scene = new Scene(root);
				stage.setScene(scene);
				stage.setTitle("实验三：抽象工厂模式");
				stage.initOwner(((Node) event.getSource()).getScene().getWindow());
				stage.initModality(Modality.WINDOW_MODAL);
				stage.show();
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

		expAdClass.setOnAction(event -> {
			Target classTarget = new AdapterOfClass();
			System.out.println("类适配器：" + classTarget.electricize());
		});

		expAdObject.setOnAction(event -> {
			Target objectTarget = new AdapterOfObject();
			System.out.println("对象适配器" + objectTarget.electricize());
		});

		expComSafe.setOnAction(event -> {
			Stage stage = new Stage();
			try {
				Pane root = FXMLLoader.load(getClass().getResource("../client/SafeComposite.fxml"));
				Scene scene = new Scene(root);
				stage.setScene(scene);
				stage.setTitle("实验五：组合模式（安全）");
				stage.initOwner(((Node) event.getSource()).getScene().getWindow());
				stage.initModality(Modality.WINDOW_MODAL);
				stage.show();
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

		expComCon.setOnAction(event -> {
			Stage stage = new Stage();
			try {
				Pane root = FXMLLoader.load(getClass().getResource("../client/ConsistentComposite.fxml"));
				Scene scene = new Scene(root);
				stage.setScene(scene);
				stage.setTitle("实验五：组合模式（一致）");
				stage.initOwner(((Node) event.getSource()).getScene().getWindow());
				stage.initModality(Modality.WINDOW_MODAL);
				stage.show();
			} catch (Exception e) {
				e.printStackTrace();
			}
		});

		expHibSpring.setOnAction(event -> {
			Stage stage = new Stage();
			try {
				Pane root = FXMLLoader.load(getClass().getResource("../client/HibernateController.fxml"));
				Scene scene = new Scene(root);
				stage.setScene(scene);
				stage.setTitle("实验七、九：hibernate+spring");
				stage.initOwner(((Node) event.getSource()).getScene().getWindow());
				stage.initModality(Modality.WINDOW_MODAL);
				stage.show();
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
	}
}
